<?php


namespace App\Model;


use Illuminate\Database\Eloquent\Model;

class TradeLog extends Model
{
    const CREATED_AT = 'createtime';
    const UPDATED_AT = null;

    //表名
    protected $table = 'trade_log';

    // 追加属性
    protected $appends = [
        'status_text'
    ];

    public function getSupplyPriceAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    protected function setSupplyPriceAttribute($value)
    {
        return bcmul($value, 100, 0);
    }

    public function getStatusTextAttribute()
    {
        $value = $this->status;
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return [1 => '冻结中', 2 => '已入账'];
    }

    /**
     * 关联订单表
     */
    public function order()
    {
        return $this->belongsto('App\Model\Order', 'order_id', 'id', '')->select('id', 'order_sn', 'status');
    }

    /**
     * 关联订单商品表
     */
    public function orderGoods()
    {
        return $this->belongsto('App\Model\OrderGoods', 'order_goods_id', 'id', '');
    }
}
